package com.example.list.simple_calendar;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.DrawableContainer;
import android.media.DrmInitData;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.list.CalendarPopWindow;
import com.example.list.R;
import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarLayout;
import com.haibin.calendarview.CalendarView;

import java.util.HashMap;
import java.util.Map;

public class SimpleCalendarActivity extends Activity implements CalendarView.OnCalendarSelectListener, CalendarPopWindow.IcalendarClick {
    CalendarView mCalendarView;

    private int mYear;
    CalendarLayout mCalendarLayout;
    private TextView tvDate1;
    private TextView tvDate2;
    private LinearLayout llDate;
    private TextView tvQuery;

    private View mPopView;
    private PopupWindow mPopupWindow;
    String sdate = "";
    String edate = "";
    private LinearLayout bg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_simple_calendar);
        Window window;
        if((window = getWindow()) != null){
            int width = ViewGroup.LayoutParams.MATCH_PARENT;
            int height = ViewGroup.LayoutParams.MATCH_PARENT;
            window.setLayout(width,height);
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        }
        initView();
    }

    private void initView() {
        mCalendarView = findViewById(R.id.calendarView);
        mCalendarLayout = findViewById(R.id.calendarLayout);
//        mCalendarView.setOnYearChangeListener(this);
        mCalendarView.setOnCalendarSelectListener(this);
        tvDate1 = (TextView) findViewById(R.id.tv_date1);
        tvDate2 = (TextView) findViewById(R.id.tv_date2);
        llDate = (LinearLayout) findViewById(R.id.ll_date);

//        mCalendarView.setWeekView(MeizuWeekView.class);
        tvQuery = (TextView) findViewById(R.id.tv_query);
        tvQuery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                llDate.setVisibility(View.GONE);
                if (sdate.isEmpty()) {
                    Toast.makeText(SimpleCalendarActivity.this, "请输入开始日期", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (edate.isEmpty()) {
                    Toast.makeText(SimpleCalendarActivity.this, "请输入结束日期", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (sdate.compareTo(edate) > 0) {
                    Toast.makeText(SimpleCalendarActivity.this, "起始日期不能比结束日期晚", Toast.LENGTH_SHORT).show();
                }

            }
        });


    }

    private void initData() {
        int year = mCalendarView.getCurYear();
        int month = mCalendarView.getCurMonth();

        Map<String, Calendar> map = new HashMap<>();
        map.put(getSchemeCalendar(year, month, 3, 0xFF40db25, "假").toString(),
                getSchemeCalendar(year, month, 3, 0xFF40db25, "假"));
        map.put(getSchemeCalendar(year, month, 6, 0xFFe69138, "事").toString(),
                getSchemeCalendar(year, month, 6, 0xFFe69138, "事"));
        map.put(getSchemeCalendar(year, month, 9, 0xFFdf1356, "议").toString(),
                getSchemeCalendar(year, month, 9, 0xFFdf1356, "议"));
        map.put(getSchemeCalendar(year, month, 13, 0xFFedc56d, "记").toString(),
                getSchemeCalendar(year, month, 13, 0xFFedc56d, "记"));
        map.put(getSchemeCalendar(year, month, 14, 0xFFedc56d, "记").toString(),
                getSchemeCalendar(year, month, 14, 0xFFedc56d, "记"));
        map.put(getSchemeCalendar(year, month, 15, 0xFFaacc44, "假").toString(),
                getSchemeCalendar(year, month, 15, 0xFFaacc44, "假"));
        map.put(getSchemeCalendar(year, month, 18, 0xFFbc13f0, "记").toString(),
                getSchemeCalendar(year, month, 18, 0xFFbc13f0, "记"));
        map.put(getSchemeCalendar(year, month, 25, 0xFF13acf0, "假").toString(),
                getSchemeCalendar(year, month, 25, 0xFF13acf0, "假"));
        map.put(getSchemeCalendar(year, month, 27, 0xFF13acf0, "多").toString(),
                getSchemeCalendar(year, month, 27, 0xFF13acf0, "多"));
        //此方法在巨大的数据量上不影响遍历性能，推荐使用
        mCalendarView.setSchemeDate(map);
    }

    private Calendar getSchemeCalendar(int year, int month, int day, int color, String text) {
        Calendar calendar = new Calendar();
        calendar.setYear(year);
        calendar.setMonth(month);
        calendar.setDay(day);
        calendar.setSchemeColor(color);//如果单独标记颜色、则会使用这个颜色
        calendar.setScheme(text);
        return calendar;
    }


    @Override
    public void onCalendarOutOfRange(Calendar calendar) {

    }

    @Override
    public void onCalendarSelect(Calendar calendar, boolean isClick) {
        String sMonth = addZero(calendar.getMonth());
        String sDay = addZero(calendar.getDay());

        String date = calendar.getYear() + sMonth + sDay;

        tvDate1.setText(date);
    }

    private String addZero(int num) {
        if (num > 0 && num < 10) {
            return "-0" + num;
        } else {
            return "-" + num;
        }
    }

    public void sdate(View view) {
        CalendarPopWindow bottomPopWinDow = new CalendarPopWindow(this);
        bottomPopWinDow.setTitle("开始日期");
        bottomPopWinDow.setDate();
        bottomPopWinDow.show();
        bottomPopWinDow.setOnCalendarClick(this);
    }

    @Override
    public void dateChoosed(String date, String title) {
        if (title.equals("开始日期")) {
            tvDate1.setText(date);
            sdate = date;
            String ss = transDate(date);
            Toast.makeText(this, ss, Toast.LENGTH_SHORT).show();
        } else if (title.equals("结束日期")) {
            tvDate2.setText(date);
            edate = date;
        }
    }

    private String transDate(String date) {
        if (date.contains("-")) {
            String[] dateNum = date.split("-");
            return dateNum[0] + dateNum[1] + dateNum[2];
        } else {
            return date;
        }
    }

    public void edate(View view) {
        CalendarPopWindow bottomPopWinDow = new CalendarPopWindow(this);
        bottomPopWinDow.setTitle("结束日期");
        bottomPopWinDow.show();
        bottomPopWinDow.setOnCalendarClick(this);
    }
}